# HTTP Server with optional libcurl support - Multi-platform Makefile
# Author: Qoder
# Description: Makefile supporting gcc, riscv64, and arm cross-compilation

# Default compiler and target
CC = gcc
TARGET = http_server

# Cross-compilation toolchains
RISCV64_CC = riscv64-anyka-linux-musl-gcc
ARM_CC = arm-nextvpu-linux-gnueabihf-gcc

# Source files
SOURCES = main.c server_functions.c cgi_functions.c
OBJECTS = $(SOURCES:.c=.o)
HEADERS = server_functions.h cgi_functions.h

# Compiler flags
CFLAGS = -Wall -Wextra -std=c99 -D_GNU_SOURCE
LDFLAGS = -pthread

# Debug flags
DEBUG_CFLAGS = -g -O0 -DDEBUG
RELEASE_CFLAGS = -O2 -DNDEBUG

# Default build type
BUILD_TYPE ?= release

# Set build-specific flags
ifeq ($(BUILD_TYPE),debug)
    CFLAGS += $(DEBUG_CFLAGS)
else
    CFLAGS += $(RELEASE_CFLAGS)
endif

# Platform-specific configurations
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
    LDFLAGS += -lrt
endif

# Default target
all: $(TARGET)

# Main target for gcc compilation
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $@ $(LDFLAGS)
	@echo "Built $(TARGET) with $(CC)"

# Cross-compilation targets
riscv64: CC = $(RISCV64_CC)
riscv64: TARGET = http_server_riscv64
riscv64: $(TARGET)_riscv64

$(TARGET)_riscv64: main_riscv64.o server_functions_riscv64.o cgi_functions_riscv64.o
	$(RISCV64_CC) $^ -o $(TARGET)_riscv64 $(LDFLAGS)
	@echo "Built $(TARGET)_riscv64 with $(RISCV64_CC)"

arm: CC = $(ARM_CC)
arm: TARGET = http_server_arm
arm: $(TARGET)_arm

$(TARGET)_arm: main_arm.o server_functions_arm.o cgi_functions_arm.o
arm: $(TARGET)_arm

$(TARGET)_arm: main_arm.o server_functions_arm.o cgi_functions_arm.o
	$(ARM_CC) $^ -o $(TARGET)_arm $(LDFLAGS)
	@echo "Built $(TARGET)_arm with $(ARM_CC)"

# Object file rules for different platforms
%.o: %.c $(HEADERS)
	$(CC) $(CFLAGS) -c $< -o $@

%_riscv64.o: %.c $(HEADERS)
	$(RISCV64_CC) $(CFLAGS) -c $< -o $@

%_arm.o: %.c $(HEADERS)
	$(ARM_CC) $(CFLAGS) -c $< -o $@

# Build all platforms
build-all: all riscv64 arm
	@echo "All platforms built successfully"

# Debug builds
debug:
	$(MAKE) BUILD_TYPE=debug

debug-riscv64:
	$(MAKE) riscv64 BUILD_TYPE=debug

debug-arm:
	$(MAKE) arm BUILD_TYPE=debug

debug-all:
	$(MAKE) build-all BUILD_TYPE=debug

# Installation
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin

install: $(TARGET)
	install -d $(BINDIR)
	install -m 755 $(TARGET) $(BINDIR)/

uninstall:
	rm -f $(BINDIR)/$(TARGET)

# Test targets
test: $(TARGET)
	@echo "Starting HTTP server test..."
	@if [ -f ./test_server.sh ]; then \
		chmod +x ./test_server.sh && ./test_server.sh; \
	else \
		echo "test_server.sh not found, running basic test..."; \
		./$(TARGET) 8080 & \
		sleep 2; \
		echo "Testing ping endpoint..."; \
		curl -s http://localhost:8080/ping || echo "Test failed - is curl installed?"; \
		pkill -f "./$(TARGET)"; \
		echo "Test completed"; \
	fi

# Clean targets
clean:
	rm -f *.o $(TARGET) $(TARGET)_riscv64 $(TARGET)_arm
	@echo "Cleaned all build files"

clean-gcc:
	rm -f *.o $(TARGET)

clean-riscv64:
	rm -f *_riscv64.o $(TARGET)_riscv64

clean-arm:
	rm -f *_arm.o $(TARGET)_arm

# Help target
help:
	@echo "Available targets:"
	@echo "  all          - Build for default platform (gcc)"
	@echo "  riscv64      - Build for RISC-V 64-bit platform"
	@echo "  arm          - Build for ARM platform"
	@echo "  build-all    - Build for all platforms"
	@echo "  debug        - Build debug version for default platform"
	@echo "  debug-riscv64- Build debug version for RISC-V platform"
	@echo "  debug-arm    - Build debug version for ARM platform"
	@echo "  debug-all    - Build debug versions for all platforms"
	@echo "  test         - Run basic functionality test"
	@echo "  install      - Install to $(PREFIX)/bin"
	@echo "  uninstall    - Remove from $(PREFIX)/bin"
	@echo "  clean        - Remove all build files"
	@echo "  clean-gcc    - Remove gcc build files"
	@echo "  clean-riscv64- Remove RISC-V build files"
	@echo "  clean-arm    - Remove ARM build files"
	@echo "  help         - Show this help message"
	@echo ""
	@echo "Build types:"
	@echo "  BUILD_TYPE=release (default) - Optimized build"
	@echo "  BUILD_TYPE=debug             - Debug build with symbols"
	@echo ""
	@echo "Example usage:"
	@echo "  make                    # Build for gcc"
	@echo "  make riscv64           # Build for RISC-V"
	@echo "  make BUILD_TYPE=debug  # Debug build"
	@echo "  make build-all         # Build all platforms"

# Dependency tracking
-include $(SOURCES:.c=.d)

%.d: %.c
	@$(CC) $(CFLAGS) -MM $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

# Phony targets
.PHONY: all riscv64 arm build-all debug debug-riscv64 debug-arm debug-all
.PHONY: install uninstall test clean clean-gcc clean-riscv64 clean-arm help

# Default goal
.DEFAULT_GOAL := all